var _ = require('lodash');
var moment = require('moment');
var { request, summary, body, description, tags } = require('koa-swagger-decorator');
var logTool = require('../Tools/LogTool');
var TestRecord = require('../Models/TestRecord');
var TestReport = require('../Models/TestReport');
const globalResponseCode = require('../base/ResponseCode');
var reportResponseCode = {
  REPORT_EXIST: { code: 50001, message: '该版本测试报告已存在' },
  DELETE_FAIL_EXIST_RECORD: { code: 50002, message: '版本下存在提测记录，测试报告不可删除' },
}
class ReportController {
  @tags(['Report'])
	@request('post', '/report/add')
	@summary('添加测试报告')
  async addTestReport(ctx){
    try{
      if(ctx.session.user){
        if(ctx.request.body.projectId&&ctx.request.body.version){
          let project = ctx.request.body.projectId
          let version = ctx.request.body.version
          let exist = await TestReport.queryReportExist(project,version)
          if(exist){
            let testReportId = await TestReport.insert(project,version);
            return ctx.body = _.merge({}, globalResponseCode.SUCCESS, { data: { id: testReportId } });
          }else{
            return ctx.body = reportResponseCode.REPORT_EXIST
          }
        }else{
          return ctx.body = globalResponseCode.PARAMS_ERROR;
        }
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN;
      }
    }catch(error){
			logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    }
  }

  @tags(['Report'])
	@request('get', '/report/getReportList')
  @summary('获取测试报告列表')
  async getReportList(ctx){
    try{
      if(ctx.session.user){
        let result = await TestReport.getTestReport()
        return ctx.body = _.merge({}, globalResponseCode.SUCCESS, { data: result });
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN;
      }
    }catch(error){
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    }
  }

  @tags(['Report'])
  @request('post','/report/deleteReport')
  @summary('删除测试报告')
  async deleteReport(ctx){
    try{
      if(ctx.session.user){
        if(ctx.request.body.projectId&& _.isInteger(ctx.request.body.projectId)&&ctx.request.body.version){
          let id = ctx.request.body.projectId
          let version = ctx.request.body.version
          let versionId = ctx.request.body.versionId
          let recordCount = await TestRecord.queryTestRecordCountByVersionId(versionId);
          if (recordCount > 0) {
            return ctx.body = reportResponseCode.DELETE_FAIL_EXIST_RECORD;
          }
          let result = await TestReport.delete(id,version)
          return ctx.body = _.merge({}, globalResponseCode.SUCCESS, { result: result });
        }else{
          return ctx.body = globalResponseCode.PARAMS_ERROR;
        }
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN;
      }

    }catch(error){
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    }
  }
  
  @tags(['Report'])
  @request('post','/report/editBasic')
  @summary('编辑测试报告基本信息')
  async editBasic(ctx){
    try{
      if(ctx.session.user){
        let report = ctx.request.body
        if(!report.id||!_.isInteger(report.id)){
          return ctx.body = globalResponseCode.PARAMS_ERROR;
        }
        let result = await TestReport.editBasicReport(report)
        return ctx.body = _.merge({}, globalResponseCode.SUCCESS, { result: result }); 
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN;
      }
    }catch(error){
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    }                                                                                                                              
  }

  @tags(['Report'])
  @request('post','/report/editText')
  @summary('编辑测试报告文本类信息')
  async editText(ctx){
    try{
      if(ctx.session.user){
        let report = ctx.request.body
        if(report.text&&report.type&& _.isInteger(report.type),report.id&&_.isInteger(report.id)){
          let result = await TestReport.editTextReport(report)
          return ctx.body = _.merge({}, globalResponseCode.SUCCESS, { result: result });
        }else{
          return ctx.body = globalResponseCode.PARAMS_ERROR;
        }
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN;
      }
    }catch(error){
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    }
  }

  @tags(['Report'])
  @request('post','/report/activeReport')
  @summary('获取当前测试报告信息')
  async activeReport(ctx){
    try{
      if(ctx.session.user){
        let id = ctx.request.body.id
        if(id&&_.isInteger(id)){
          let result = await TestReport.queryActiveReport(id)
          return ctx.body = _.merge({}, globalResponseCode.SUCCESS, { data: result });
        }else{
          return ctx.body = globalResponseCode.PARAMS_ERROR;
        }
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN; 
      }

    }catch(error){
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    } 
  }

  @tags(['Report'])
  @request('post','/report/setTestData')
  @summary('更新测试数据')
  async updateTestData(ctx){
    try{
      if(ctx.session.user){
        let pname = ctx.request.body.pname
        let version = ctx.request.body.version
        if(pname&&version){
          let result = await TestReport.setTestData(pname,version)
          return ctx.body = _.merge({}, globalResponseCode.SUCCESS, { data: result });
        }else{
          return ctx.body = globalResponseCode.PARAMS_ERROR;
        }
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN; 
      }

    }catch(error){
      console.log(error)
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    } 
  }

  @tags(['Report'])
  @request('post','/testreport/ReleaseReport')
  @summary('发布测试报告')
  async updateReportState(ctx){
    try{
      if(ctx.session.user){
        let id = ctx.request.body.id
        let realname =ctx.session.user.realName
       // console.log('xxx',realname)
        if(id&&_.isInteger(id)){
          let result = await TestReport.setTestState(id,realname,0)
          return ctx.body = _.merge({}, globalResponseCode.SUCCESS,  result );
        }else{
          return ctx.body = globalResponseCode.PARAMS_ERROR;
        }
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN; 
      }

    }catch(error){
     // console.log('error1',error)
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    } 
  }

  @tags(['Report'])
  @request('post','/testreport/updateTestStatus')
  @summary('更新测试状态')
  async setTestStatus(ctx){
    try{
      if(ctx.session.user){
        let name = ctx.request.body.name
        let version = ctx.request.body.version
        if(name&&version){
          let result = await TestReport.updateTestStatus(name,version,3)
          return ctx.body = _.merge({}, globalResponseCode.SUCCESS,  result );
        }else{
          return ctx.body = globalResponseCode.PARAMS_ERROR;
        }
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN; 
      }

    }catch(error){
      console.log(error)
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    } 
  }


  @tags(['Report'])
  @request('get','/testreport/getBasicData')
  @summary('获取测试报告基础选项列表')
  async getBasicDataArray(ctx){
    try{
      if(ctx.session.user){
        let result = await TestReport.getDataArray()
        return ctx.body = _.merge({}, globalResponseCode.SUCCESS,  {data:result} );
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN; 
      }

    }catch(error){
      console.log(error)
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    } 
  }

  @tags(['Report'])
  @request('post','/testreport/addBasicData')
  @summary('插入基础选项')
  async addBasicData(ctx){
    try{
      if(ctx.session.user){
        let array = ctx.request.body.newData
        if(array&&array.length>0){
          let res = await TestReport.insertBasicData(array)
        }
        return ctx.body = _.merge({}, globalResponseCode.SUCCESS);
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN; 
      }

    }catch(error){
      console.log(error)
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    } 
  }

  @tags(['Report'])
  @request('post','/testreport/deleteBasicData')
  @summary('删除基础选项')
  async deleteBasicData(ctx){
    try{
      if(ctx.session.user){
        let array = ctx.request.body.deleteData
        if(array&&array.length>0){
          let res = await TestReport.deleteBasicData(array)
        }
        return ctx.body = _.merge({}, globalResponseCode.SUCCESS);
      }else{
        return ctx.body = globalResponseCode.NO_LOGIN; 
      }

    }catch(error){
      console.log(error)
      logTool.logError(ctx, error);
			return ctx.body = globalResponseCode.SYSTEM_EXCEPTION;
    } 
  }


}

module.exports = new ReportController();